package WorldMap;

import java.util.ArrayList;

/*
 * this is a Queue class used by the BFS
 */



/**
 * The Class TQueue.
 */
public class TQueue {
	
	private int Size;
	private int MyCount;
	private int Hptr;
	private int Tptr;
	private ArrayList<Integer> Q;


	public TQueue(int QueueSize)
	{
		Size = QueueSize;
		MyCount = 0;
		Hptr =  0;
		Tptr = 0;
		Q = new ArrayList<Integer>(Size);
		for (int i = 0; i < Size; i++)
		{
			Q.add(0);
		}
	}
	
	/**
	 * Enqueue.
	 * 
	 * @param NewNode the new node
	 * 
	 * @throws Exception the exception
	 */
	public void Enqueue(Integer NewNode) throws Exception
	{
		
		if (IsFull())
		{
			//throw EX
			throw new Exception("Queue is Full");
		}
		else
		{
			Q.set(Tptr, NewNode);
			MyCount += 1;
		}
	 
       // Bump tail pointer to next available location
       Tptr = Tptr + 1;
       if (Tptr >= Q.size() )
       {
    	   Tptr = 0;   
       }
	}

	public Integer Dequeue() throws Exception
	{
			Integer item = null;

          if (IsEmpty())
        	  {
        	     //Throw New Exception("Queue is empty !")

  			throw new Exception("Queue is Empty");
        	  }
          else
          {
        	  

        	  item = Q.get(Hptr);
              MyCount -= 1;

              //' Bump head pointer, wrap to beginning if it reaches the end
              Hptr = Hptr + 1;
              if( Hptr >= Q.size())
            	  {
            	  Hptr = 0;
            	  }
          }

		return item;	
	}

	public boolean IsEmpty()
	{
		return (MyCount == 0);
	}
	
	public boolean IsFull()
	{
		return (MyCount == Size);
	}
 
	public int Count()
	{
		return MyCount;
	}
}
